import { createRouter, createWebHistory } from 'vue-router'
import HomePage from '@/pages/HomePage.vue'
import UserLoginPage from '@/pages/user/UserLoginPage.vue'
import UserRegisterPage from '@/pages/user/UserRegisterPage.vue'
import UserManagePage from '@/pages/admin/UserManagePage.vue'
import PictureManagePage from "@/pages/admin/PictureManagePage.vue";
import PictureDetailPage from "@/pages/pictureDetailPage.vue";
import {useLoginUserStore} from "@/stores/useLoginUserstore";
import AddPicturePage from "@/pages/AddPicturePage.vue";
import AddPictureBatchPage from "@/pages/AddPictureBatchPage.vue";
import SpaceManagePage from "@/pages/admin/SpaceManagePage.vue";
import AddSpacePage from "@/pages/AddSpacePage.vue";
import MySpacePage from "@/pages/MySpacePage.vue";
import SpaceDetailPage from "@/pages/SpaceDetailPage.vue";
import SpaceAnalyzePage from "@/pages/SpaceAnalyzePage.vue";
import UserInfoPage from "@/pages/user/UserInfoPage.vue";
import SpaceUserManagePage from "@/pages/admin/SpaceUserManagePage.vue";
import SearchPicturePage from "@/pages/searchPicturePage.vue";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomePage,
    },
    {
      path: '/user/login',
      name: '用户登录',
      component: UserLoginPage,
    },
    {
      path: '/user/register',
      name: '用户注册',
      component: UserRegisterPage,
    },
    {
      path: '/admin/userManage',
      name: '用户管理',
      component: UserManagePage,
    },
    {
      path: '/add_picture',
      name: '创建图片',
      component: AddPicturePage,
    },
    {
      path: '/admin/pictureManage',
      name: '图片管理',
      component: PictureManagePage,
    },
    {
      path: '/picture/:id',
      name: '图片详情',
      component: PictureDetailPage,
      props: true,
    },
    {
      path: '/add_picture/batch',
      name: '批量创建图片',
      component: AddPictureBatchPage,
    },
    {
      path: '/admin/spaceManage',
      name: '空间管理',
      component: SpaceManagePage,
    },
    {
      path: '/add_space',
      name: '创建空间',
      component: AddSpacePage,
    },
    {
      path: '/my_space',
      name: '我的空间',
      component: MySpacePage,
    },
    {
      path: '/space/:id',
      name: '空间详情',
      component: SpaceDetailPage,
      props: true,
    },
    {
      path: '/space_analyze',
      name: '空间分析',
      component: SpaceAnalyzePage,
    },
    {
      path: '/user/getUserInfo',
      name: '个人中心',
      component: UserInfoPage,
    },
    {
      path: '/spaceUserManage/:id',
      name: '空间成员管理',
      component: SpaceUserManagePage,
      props: true,
    },
    {
      path: '/search_picture',
      name: '图片搜索',
      component: SearchPicturePage,
    },



  ],
})
// 路由守卫：每次跳转前验证登录状态
router.beforeEach(async (to, from, next) => {
  const loginStore = useLoginUserStore();

  // 1. 调用后端接口，校验 Session 是否有效
  await loginStore.fetchLoginUser();

  // 2. 根据校验结果决定是否允许访问
  const isLoggedIn = loginStore.loginUser.userName !== "未登录";
  if (to.meta.requiresAuth && !isLoggedIn) {
    // 未登录且访问受保护页面，跳转到登录页
    next("/login");
  } else {
    // 已登录或访问无需登录的页面，正常跳转
    next();
  }
});
export default router

